package academic.emailClient.control.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import academic.emailClient.control.MessageMapper;
import academic.emailClient.model.EmailClientModel;
import academic.emailClient.model.entities.Filter;
import academic.emailClient.model.entities.FilterBuilder;
import academic.emailClient.view.EmailClientViewInterface;

public class CreateFilterListener implements ActionListener {

	private EmailClientViewInterface view;
	private EmailClientModel model;


	/**
	 * CreateFilterListener Constructor
	 * @param view
	 * @param model
	 */
	public CreateFilterListener(EmailClientViewInterface view, EmailClientModel model) {
		super();
		this.view = view;
		this.model = model;
	}

	/**
	 * handle the create filter action
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		FilterBuilder filter = new FilterBuilder();
		Filter newFilter = filter.withName(view.getFilterName())
				.withDestinationFolderPath(view.getFilterPath())
				.withQuery(view.takingTheValues())
				.build();

		model.createFilter(newFilter);
		view.updateMailBoxList(MessageMapper.mapToMessageViewList(model.getIboxMessages()));
	}
}
